package com.android.myj.view;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.CharacterStyle;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.myj.R;
import com.android.myj.db.DataHelper;
import com.android.myj.model.User;
import com.android.utils.AsyncImageLoader;
import com.android.utils.TimeUtil;
import com.android.utils.AsyncImageLoader.ImageCallback;
import com.tencent.weibo.api.T_API;
import com.tencent.weibo.beans.OAuth;
import com.tencent.weibo.utils.WeiBoConst;

public class WeiboDetail extends Activity {
	private DataHelper dataHelper;
	private Handler handler;
	private AsyncImageLoader asyncImageLoader;
	private GetDetailThread thread;
	private String weiboid;
	private String returnJsonStr;
	private JSONObject dataObj;
	private ImageView show_headicon;
	private ImageView show_image;
	private TextView show_count_mcount;
	private ImageView show_delete;
	private TextView show_nick;
	private TextView show_email;
	private TextView show_origtext;
	private TextView show_time;
	private TextView show_from;
	private Button to_userinfo_btn;
	private TextView show_rebroad_btn;
	private TextView show_dialog_btn;
	private TextView show_remark_btn;
	private RelativeLayout show_top;
	private View weibodetail_bottom3_bar;
	private JSONObject source = null;
	SharedPreferences preferences;
	private OAuth oauth;
	private User user;
	private T_API tAPI;
	String userName;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.weibo_detail);
		setUpViews();// ����view
		setUpListeners();// ����listenter
		setToken();
		asyncImageLoader = AsyncImageLoader.getInstance();		
		Intent intent = getIntent();
		weiboid = intent.getStringExtra("weiboid");
		dataHelper = DataHelper.getInstance(WeiboDetail.this);
		List<User> userList = dataHelper.GetUserList(false);
		user = DataHelper.getUserByName(userName,userList);
		handler = new DealHandler();
		thread = new GetDetailThread();
		thread.start();// ����һ���̻߳�ȡ���
	}

	private void setUpViews() {
		show_headicon = (ImageView) findViewById(R.id.show_headicon);
		show_delete = (ImageView) findViewById(R.id.show_delete);
		show_nick = (TextView) findViewById(R.id.show_nick);
		show_email = (TextView) findViewById(R.id.show_email);
		show_origtext = (TextView) findViewById(R.id.show_origtext);
		show_image = (ImageView) findViewById(R.id.show_image);
		show_count_mcount = (TextView) findViewById(R.id.show_count_mcount);
		show_time = (TextView) findViewById(R.id.show_time);
		show_from = (TextView) findViewById(R.id.show_from);
		to_userinfo_btn = (Button) findViewById(R.id.to_userinfo_btn);
		weibodetail_bottom3_bar = (View) findViewById(R.id.weibo_detail_bottom_bar);
		show_rebroad_btn = (TextView) weibodetail_bottom3_bar
				.findViewById(R.id.show_rebroad_btn);
		show_dialog_btn = (TextView) weibodetail_bottom3_bar
				.findViewById(R.id.show_dialog_btn);
		show_remark_btn = (TextView) weibodetail_bottom3_bar
				.findViewById(R.id.show_remark_btn);
		show_top = (RelativeLayout) findViewById(R.id.show_top);
	}

	private void setUpListeners() {
		show_top.setOnClickListener(new ToUserInfo());
		to_userinfo_btn.setOnClickListener(new ToUserInfo());
		show_image.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// ���ͼ�������.
			}
		});

		show_count_mcount.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// ��΢����ת���͵���
				Toast.makeText(WeiboDetail.this, "����ʾ��΢����ת���͵����б�",
						Toast.LENGTH_SHORT).show();
			}
		});

		show_rebroad_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {// ת������΢��
				Intent intent = new Intent(WeiboDetail.this, AddWeibo.class);
				try {
					if (source != null) {
						intent.putExtra("tip", "转播"
								+ source.getString("nick"));
					} else {
						intent.putExtra("tip", "转播"
								+ dataObj.getString("nick"));
					}
					if (dataObj.getString("origtext") != null
							&& !"".equals(dataObj.getString("origtext"))) {
						intent.putExtra("content", "|| @"
								+ dataObj.getString("nick") + ": "
								+ dataObj.getString("origtext"));
						intent.putExtra("reid", dataObj.getString("id"));
					} else {
						intent.putExtra("content", "|| @"
								+ source.getString("nick") + ": ");
						intent.putExtra("reid", source.getString("id"));
					}
					intent.putExtra("from_flag", "rebroad");
				} catch (JSONException e) {
					e.printStackTrace();
				}
				startActivity(intent);
			}
		});

		show_dialog_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {// �Ի�����΢��������
				Intent intent = new Intent(WeiboDetail.this, AddWeibo.class);
				try {
					intent.putExtra("tip", "对话" + dataObj.getString("nick"));
					intent.putExtra("to", dataObj.getString("name"));// �Ի��˵�name
					intent.putExtra("from_flag", "private");
					intent.putExtra("content", "");
				} catch (JSONException e) {
					e.printStackTrace();
				}
				startActivity(intent);
			}
		});

		show_remark_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {// ��������΢��
				Intent intent = new Intent(WeiboDetail.this, AddWeibo.class);
				try {
					if (source != null) {
						intent.putExtra("tip", "点评 "
								+ source.getString("nick"));
					} else {
						intent.putExtra("tip", "点评  "
								+ dataObj.getString("nick"));
					}
					if (dataObj.getString("origtext") != null
							&& !"".equals(dataObj.getString("origtext"))) {
						intent.putExtra("content", "|| @"
								+ dataObj.getString("nick") + ": "
								+ dataObj.getString("origtext"));
						intent.putExtra("reid", dataObj.getString("id"));
					} else {
						intent.putExtra("content", "|| @"
								+ source.getString("nick") + ": ");
						intent.putExtra("reid", source.getString("id"));
					}
					intent.putExtra("from_flag", "comment");
				} catch (JSONException e) {
					e.printStackTrace();
				}
				startActivity(intent);
			}
		});
	}
class ToUserInfo implements OnClickListener{

	@Override
	public void onClick(View v) {
		Intent intent = new Intent(WeiboDetail.this, UserInfo.class);
		try {
			intent.putExtra("name", dataObj.getString("name"));
			intent.putExtra("nick", dataObj.getString("nick"));
			intent.putExtra("origtext", dataObj.getString("origtext"));
			intent.putExtra("timestamp", TimeUtil
					.getStandardTime(dataObj.getLong("timestamp")));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		startActivity(intent);// ��ת���û���Ϣ����		
	}
	
}
	class GetDetailThread extends Thread {
		@Override
		public void run() {
			tAPI = new T_API();
			try {
				returnJsonStr = tAPI.show(oauth,
						WeiBoConst.ResultType.ResultType_Json, weiboid);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Message msg = handler.obtainMessage();
			handler.sendMessage(msg);
		}
	}

	class DealHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			Drawable cachedImage;
			try {
				dataObj = new JSONObject(returnJsonStr).getJSONObject("data");

				cachedImage = asyncImageLoader.loadDrawable(dataObj
						.getString("head")
						+ "/100", show_headicon, new ImageCallback() {
					@Override
					public void imageLoaded(Drawable imageDrawable,
							ImageView imageView, String imageUrl) {
						imageView.setImageDrawable(imageDrawable);
					}
				});
				if (cachedImage == null) {
					show_headicon.setImageResource(R.drawable.icon);
				} else {
					show_headicon.setImageDrawable(cachedImage);
				}

				String count_mcount_text = "转播和点评("
						+ (dataObj.getInt("count") + dataObj.getInt("mcount"))
						+ ")";// ���»���
				SpannableStringBuilder underlineSpannable = new SpannableStringBuilder(
						count_mcount_text);
				CharacterStyle span = new UnderlineSpan();
				underlineSpannable.setSpan(span, 0, count_mcount_text.length(),
						Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				show_count_mcount.setText(underlineSpannable);

				show_nick.setText(dataObj.getString("nick"));
				show_email.setText("@" + dataObj.getString("name"));
				show_origtext.setText(dataObj.getString("origtext"));
				show_time.setText(TimeUtil.getStandardTime(dataObj
						.getLong("timestamp")));
				show_from.setText("来自" + dataObj.getString("from"));
				if (dataObj.getString("nick").equals(user.getUserName())) {
					show_delete.setVisibility(View.VISIBLE);
				}

				JSONArray imageArray = dataObj.optJSONArray("image");// ����΢����ͼƬ���ݣ�����ʾ����
				if (imageArray != null && imageArray.length() > 0) {
					String imageUrl = imageArray.optString(0) + "/460";// Ϊʲô��/460����Ѷ�涨�ģ�֧��160��2000��460����һЩ���ǲ�ס��
					Drawable drawable = asyncImageLoader.loadDrawable(imageUrl,
							show_image, new ImageCallback() {
								@Override
								public void imageLoaded(Drawable imageDrawable,
										ImageView imageView, String imageUrl) {
									imageView.setImageDrawable(imageDrawable);
								}
							});
					if (drawable == null) {
						show_image.setImageResource(R.drawable.icon);
					}else {
						show_image.setImageDrawable(drawable);
					}
					show_image.setVisibility(View.VISIBLE);
				}

				if (!"null".equals(dataObj.getString("source"))) {
					source = dataObj.getJSONObject("source");
				}

			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}

	private void setToken() {
		preferences = getSharedPreferences("default_user",
				Activity.MODE_PRIVATE);
		 userName = preferences.getString("user_default_nick", "");
		oauth = new OAuth();
		oauth = OAuth.setToken(WeiboDetail.this, userName);
	}
}
